package com.lu.sb210225.fdfs;

import com.github.tobato.fastdfs.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.MateData;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Set;

/**
 * fdfs工具类
 *
 * @author liushuai
 * @date 2020-3-31 9:54
 */
@Component
public class FdfsClient {

    @Resource(name = "defaultFastFileStorageClient")
    private FastFileStorageClient storageClient;

    @Resource
    private FdfsWebServer fdfsWebServer;

    /**
     * 上传文件
     *
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException file.getInputStream()
     */
    public synchronized String uploadFile(MultipartFile file) throws IOException {
        return uploadFile(file.getInputStream(), file.getOriginalFilename());
    }

    /**
     * 上传文件
     *
     * @param file 文件
     * @return 文件访问地址
     * @throws FileNotFoundException|IOException inputStream.available()|new FileInputStream(file)
     */
    public synchronized String uploadFile(File file) throws IOException {
        return uploadFile(new FileInputStream(file), file.getName());
    }

    /**
     * @param inputStream 文件流
     * @param fileName 文件名
     * @return 文件访问地址
     * @throws IOException inputStream.available()
     */
    public synchronized String uploadFile(InputStream inputStream, String fileName) throws IOException {
        return uploadFile(inputStream, fileName, null);
    }

    /**
     * @param inputStream 文件流
     * @param fileName 文件名
     * @param mateDataSet 文件内容元数据
     * @return 文件访问地址
     * @throws IOException inputStream.available()
     */
    public synchronized String uploadFile(InputStream inputStream, String fileName, Set<MateData> mateDataSet) throws IOException {
        StorePath storePath = storageClient.uploadFile(inputStream, inputStream.available(), FilenameUtils.getExtension(fileName), mateDataSet);
        return getResAccessUrl(storePath);
    }

    /**
     * @param content 文件内容
     * @param fileExtension 文件拓展名
     * @param mateDataSet 文件内容元数据
     * @return String
     */
    public synchronized String uploadFile(String content, String fileExtension, Set<MateData> mateDataSet) {
        byte[] buff = content.getBytes(StandardCharsets.UTF_8);
        ByteArrayInputStream stream = new ByteArrayInputStream(buff);
        StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, mateDataSet);
        return getResAccessUrl(storePath);
    }

    private synchronized String getResAccessUrl(StorePath storePath) {
        return StringUtils.defaultString(fdfsWebServer.getWebServerUrl(),"") + storePath.getFullPath();
    }

    /**
     * 下载文件
     *
     * @param filePath 文件path eg: group1/M00/00/67/wKgEXl6BvU5691TcAAfAeP1E2EU286.jpg
     * @return byte[]
     */
    public synchronized byte[] downloadFile(String filePath) {
        StorePath storePath = StorePath.praseFromUrl(filePath);
        return storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
    }

    /**
     * 删除文件
     *
     * @param filePath 文件访问地址
     */
    public synchronized void deleteFile(String filePath) {
        storageClient.deleteFile(filePath);
    }

    /**
     * 删除文件
     *
     * @param storePath storePath
     */
    public synchronized void deleteFile(StorePath storePath) {
        storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
    }

    /**
     *
     *
     * @param filePath filePath
     * @return StorePath
     */
    public synchronized StorePath praseFromUrl(String filePath) {
        return StorePath.praseFromUrl(filePath);
    }
}
